package com.syh.jn.scm.service.impl.supplier.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.exception.IllegalParameterException;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.system.SupplierAccount;
import com.syh.jn.scm.domain.supplier.event.system.AccountEvent;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierAccountRepository;
import com.syh.jn.scm.dto.supplier.system.supplierAccount.SupplierAccountMto;
import com.syh.jn.scm.dto.supplier.system.supplierAccount.SupplierAccountSto;
import com.syh.jn.scm.dto.supplier.system.supplierAccount.SupplierAccountVo;
import com.syh.jn.scm.service.api.supplier.system.SupplierAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SupplierAccountServiceImpl extends AbstractServiceImpl<SupplierAccountSto, SupplierAccountMto, SupplierAccountVo, SupplierAccount, Long> implements SupplierAccountService {

  @Autowired
  private SupplierAccountRepository repository;

  @Override
  protected AggregateRepository<SupplierAccount, Long> getAggregateRepository() {
    return this.repository;
  }

  @Override
  public Long save(SupplierAccountSto accountSto) {
    SupplierAccount supplierAccount = new SupplierAccount(accountSto);
    return repository.add(supplierAccount);
  }

  @Override
  public void update(SupplierAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ExtAssert.notNull(mto.getId(), "id为空");
    SupplierAccount supplierAccount = repository.getById(mto.getId());
    if (supplierAccount != null) {
      supplierAccount.modify(mto);
      repository.update(supplierAccount);
    }
  }

  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");
    SupplierAccount supplierAccount = repository.getById(id);
    SupplierAccount eventAccount = new SupplierAccount();
    ExtBeanUtils.copyProperties(supplierAccount, eventAccount);
    if (supplierAccount != null) {
      supplierAccount.destory();
      repository.removeById(id);
      this.publicEvent(AccountEvent.buildAccountDeleted(new Object[]{eventAccount}));
    }
  }


  @Override
  public void changeEable(Long accountId, Short enable) {
    ExtAssert.notNull(accountId, "id为空");
    SupplierAccount supplierAccount = repository.getById(accountId);
    SupplierAccount eventAccount = new SupplierAccount();
    ExtBeanUtils.copyProperties(supplierAccount, eventAccount);
    if (supplierAccount != null) {
      supplierAccount.changeEnable(enable);
      repository.update(supplierAccount);
      if (enable.equals(Short.valueOf("-1")) && eventAccount.getLoginPhoneId() != null)
        this.publicEvent(AccountEvent.buildAccountDisabled(new Object[]{eventAccount}));
    }
  }


  @Override
  public void setRole(Long accountId, Long roldId) {
    ExtAssert.notNull(roldId, "id为空");
    SupplierAccount supplierAccount = repository.getById(accountId);
    if (supplierAccount != null) {
      supplierAccount.changeRole(roldId);
      repository.update(supplierAccount);
    }
  }

  @Override
  public SupplierAccountVo verifyAccountPassword(String loginAccount, String password) {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(password, "密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    SupplierAccount supplierAccount = repository.getOne(wrapper);
    if (supplierAccount == null) {
      return null;
    }
    if (PasswordUtils.verify(password, supplierAccount.getLoginPassword())) {
      return repository.getOneView(wrapper);
    } else {
      return null;
    }
  }

  @Override
  public void updataPassword(String loginAccount, String oldPassword, String newPassword) throws IllegalParameterException {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(oldPassword, "旧密码为空");
    ExtAssert.notNull(newPassword, "新密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    SupplierAccount supplierAccount = repository.getOne(wrapper);
    ExtAssert.notNull(supplierAccount, "找不到该账户");
    if (PasswordUtils.verify(oldPassword, supplierAccount.getLoginPassword())) {
      supplierAccount.setLoginPassword(newPassword);
      repository.update(supplierAccount);
    } else {
      throw new IllegalParameterException("旧密码不正确");
    }

  }

  @Override
  public void bindingLoginPhone(Long id, Long phoneId) {
    SupplierAccount supplierAccount = repository.getById(id);
    supplierAccount.bindingLoginPhone(phoneId);
    repository.update(supplierAccount);
  }

  @Override
  public void untyingLoginPhone(Long id) {
    SupplierAccount supplierAccount = repository.getById(id);
    SupplierAccount eventAccount = new SupplierAccount();
    ExtBeanUtils.copyProperties(supplierAccount, eventAccount);
    supplierAccount.untyingLoginPhone();
    repository.update(supplierAccount);
    this.publicEvent(AccountEvent.buildAccountPhoneUntyed(new Object[]{eventAccount}));
  }

  @Override
  public boolean identityAdmin(Long id) {
    Short admin = repository.getAdminById(id);
    if (admin != null && admin.equals(Short.valueOf("1"))) {
      return true;
    }
    return false;
  }
}
